package com.cloudm.technician.ui.modules.cloudm_box.plate_number

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.cloudm.technician.R
import com.cloudm.technician.data.model.ScanResultInfo
import com.cloudm.technician.extensions.inflate
import com.kelin.scrollablepanel.library.PanelAdapter
import kotlinx.android.synthetic.main.item_scan_result.view.*
import kotlinx.android.synthetic.main.item_scan_result_data.view.*
import kotlinx.android.synthetic.main.item_scan_result_row.view.*

/**
 * 作者: wesley
 * 时间: 2017/12/27
 * 描述: 适配器
 */
class PlateNumberOilAdapter : PanelAdapter() {

    private val TYPE_0 = 0
    private val TYPE_1 = 1
    private val TYPE_2 = 2
    private val TYPE_3 = 3

    private val listRow: ArrayList<ScanResultInfo> = arrayListOf()
    private val listColumn: ArrayList<ScanResultInfo> = arrayListOf()
    private val list: ArrayList<ArrayList<String?>> = arrayListOf()

    override fun getRowCount(): Int = listRow.size + 1

    override fun getColumnCount(): Int = listColumn.size

    fun updateColumdata(list: ArrayList<ScanResultInfo>) {
        listColumn.clear()
        listColumn.addAll(list)
    }

    fun updateRowAndContent(listRow: ArrayList<ScanResultInfo>,
                            listContent: ArrayList<ArrayList<String?>>) {
        this.listRow.clear()
        list.clear()
        this.listRow.addAll(listRow)
        list.addAll(listContent)
    }

    override fun getItemViewType(row: Int, column: Int): Int {
        if (row == 0 && column == 0) {
            return TYPE_0
        }

        if (column == 0) {
            return TYPE_1
        }

        if (row == 0) {
            return TYPE_2
        }

        return TYPE_3
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, row: Int, column: Int) {
        val viewType = getItemViewType(row, column)
        when (viewType) {
            TYPE_0 -> setData(holder)
            TYPE_1 -> setRowData(row, holder)
            TYPE_2 -> setColumnData(column, holder)
            TYPE_3 -> setItemData(row, column, holder)
            else -> {
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? {
        when (viewType) {
            TYPE_0 -> {
                val view = parent.inflate(R.layout.item_scan_result)
                return ItemViewHolder(view)
            }

            TYPE_1 -> {
                val view_row = parent.inflate(R.layout.item_scan_result_row)
                return ItemTypeRowViewHolder(view_row)
            }

            TYPE_2 -> {
                val view_column = parent.inflate(R.layout.item_scan_result)
                return ItemTypeColumnViewHolder(view_column)
            }

            TYPE_3 -> {
                val view_data = parent.inflate(R.layout.item_scan_result_data)
                return ItemDataViewHolder(view_data)
            }

            else -> {
            }
        }

        return null
    }

    //0行0列
    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    //行
    class ItemTypeRowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    //列
    class ItemTypeColumnViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    //中间的数据
    class ItemDataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)


    private fun setData(viewHolder: RecyclerView.ViewHolder?) {
        if (viewHolder != null && viewHolder is ItemViewHolder && listColumn.size > 0) {
            viewHolder.itemView.scan_result_value.text = listColumn[0].value
        }
    }

    private fun setRowData(row: Int, viewHolder: RecyclerView.ViewHolder?) {
        val realRow = row - 1
        if (realRow >= 0 && listRow.size > realRow && viewHolder != null && viewHolder is ItemTypeRowViewHolder) {
            viewHolder.itemView.scan_result_value_row.text = listRow[realRow].value
        }
    }

    private fun setColumnData(column: Int, viewHolder: RecyclerView.ViewHolder?) {
        if (column >= 0 && listColumn.size > column && viewHolder != null && viewHolder is ItemTypeColumnViewHolder) {
            viewHolder.itemView.scan_result_value.text = listColumn[column].value
        }
    }

    private fun setItemData(row: Int, column: Int, viewHolder: RecyclerView.ViewHolder?) {
        val realRow = row - 1
        val realColumn = column - 1
        if (realRow >= 0 && realColumn >= 0 && viewHolder != null && viewHolder is ItemDataViewHolder && list.size > realRow && list[realRow].size > realColumn) {
            viewHolder.itemView.scan_result_value_item_data.text = list[realRow][realColumn]
        }
    }
}